// pages/confirmorder/subaddress.js
import authorize from '../../utils/authorize.js';
import Router from '../../utils/router';
import util from '../../utils/util.js';
import address from '../../services/free.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ceorid: '6',
    datas: {},
    money: '', //商品价格
    address: null, //地址
    goods_img: '', //商品图片
    show: false,
    btn: false, //禁止多次提交订单
    orid:'',//订单id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      ceorid: options.ceorid,
      address: wx.getStorageSync('address') || null
    })
    this.createHtml();
  },
  createHtml() {
    let _this = this;
    let params = {
      ceorid: _this.data.ceorid
    }
    address.getdetail(params, res => {
      console.log(res)
      if (res.code == 1) {
        _this.setData({
          datas: res.data,
          money: Number(res.data.orders.total_money).toFixed(2),
          goods_img: res.data.orders.goods_pic
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  //选择收货地址
  chooseAddress() {
    let _this = this;
    authorize('address', (b) => {
      if (b) {
        wx.chooseAddress({
          success(res) {
            res.txt = `${res.provinceName} ${res.cityName} ${res.countyName}${res.detailInfo}`
            wx.setStorageSync('address', res);
            _this.setData({
              address: res
            });
          },
          fail(res) {}
        });
      }
    })
  },
  //提交订单信息
  subThis() {
    let _this = this;
    if (_this.data.address == null) {
      wx.showToast({
        title: '请设置收货地址',
        icon: 'none'
      })
      return false;
    }
    if (_this.data.btn) {
      return false
    }

    this.setData({
      btn: true
    })
    let params = {
      ceorid: _this.data.ceorid,
      address: _this.data.address.txt,
      province_name: _this.data.address.provinceName,
      city_name: _this.data.address.cityName,
      area_name: _this.data.address.countyName,
      buyer_name: _this.data.address.userName,
      tel: _this.data.address.telNumber,
      goods_img: _this.data.goods_img,
      wechat_scene: getApp().globalData.scene,
      order_source: getApp().globalData.sharerecord
    }
    address.createorder(params, res => {
      _this.setData({
        btn: false
      })
      if (res.code == 1) {
        _this.setData({
          show: true,
          orid:res.data.orid
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  //去到列表页（继续拿）
  goList() {
    Router.go('freecharege', ``)
  },
  //去到订单列表页
  goOrder() {
    let _this=this;
    Router.go('orderDetail', `orid=${_this.data.orid}`)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})